/**
 * 文件：DeleteStuPanel.java
 * 说明：用于删除学生
 * 作者：古通基
 * 时间：2011-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DeleteStuPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -7578391667142650572L;
	private JLabel lno = new JLabel("学号：");                                                  //静态标签
	private JLabel lname = new JLabel("姓名：");
	private JLabel lsex = new JLabel("性别：");              
	private JLabel lnation = new JLabel("民族：");        
	private JLabel lcollege = new JLabel("院系：");       
	private JLabel lspecialty = new JLabel("专业：");    
	private JLabel lbirthday = new JLabel("生日：");     
	private JLabel lgrade = new JLabel("年级：");        
	
	private JTextField no = new JTextField("请输入五位学号",10);                     //文本输入
	private JTextField name = new JTextField("",10);                
	private JTextField sex = new JTextField("",10);            
	private JTextField nation = new JTextField("",10);       
	private JTextField college = new JTextField("",10);      
	private JTextField specialty = new JTextField("",10);   
	private JTextField birthday = new JTextField("",10);    
	private JTextField grade = new JTextField("",10);       
                      
    private JButton query = new JButton("查询");                            //声明按钮
    private JButton delete = new JButton("删除");   
    private JButton reset = new JButton("重置"); 
    
    private JPanel JPcomon1 = new JPanel();                                   //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPcomon7 = new JPanel();
    private JPanel JPcomon8 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public DeleteStuPanel()                                                               //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lsex.setHorizontalAlignment(SwingConstants.CENTER);
    	lcollege.setHorizontalAlignment(SwingConstants.CENTER);
    	lspecialty.setHorizontalAlignment(SwingConstants.CENTER);
    	lgrade.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lsex);
    	JPcomon3.add(sex);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lnation);
    	JPcomon4.add(nation);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(lcollege);
    	JPcomon5.add(college);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(lspecialty);
    	JPcomon6.add(specialty);
    	JPcomon7.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon7.add(lbirthday);
    	JPcomon7.add(birthday);
    	JPcomon8.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon8.add(lgrade);
    	JPcomon8.add(grade);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));    	
    	JPbutton.add(delete);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPcomon7);
    	this.add(JPcomon8);
    	this.add(JPbutton);
    	
		name.setEditable(false);
		sex.setEditable(false);
		nation.setEditable(false);
		college.setEditable(false);
		specialty.setEditable(false);
		birthday.setEditable(false);
		grade.setEditable(false);    
		delete.setEnabled(false);
    	
    	query.addActionListener(this);                                                        //事件监听        
    	delete.addActionListener(this);
    	reset.addActionListener(this);
    	no.addMouseListener(this);
    }   
    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
		}
		else if(e.getActionCommand().equals("删除"))
		{
			DeleteFromDataBase();
		}
		else if(e.getActionCommand().equals("重置"))
		{
			Reset();
		}
	}	
	
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL stu = null;
			stu = new BaseSQL();
			String sql = "select * from student where sno = '"+Integer.parseInt(no.getText())+"'";  
			
		    rs = stu.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));
    				sex.setText(rs.getString("sex"));
    				nation.setText(rs.getString("nation"));
    				college.setText(rs.getString("college"));
    				specialty.setText(rs.getString("specialty"));
    				birthday.setText(rs.getString("birthday"));
    				grade.setText(rs.getString("grade"));	
    				no.setEditable(false);	
    				delete.setEnabled(true);
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在学号为："+no.getText()+" 的学生","查询错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
	private void DeleteFromDataBase()
	{
		try
		{
			BaseSQL stu = null;
			stu = new BaseSQL();
			String sql = "delete from student where sno='"+Integer.parseInt(no.getText())+"'";
			stu.executeDML(sql);
			JOptionPane.showMessageDialog(this, "已经删除学号为："+Integer.parseInt(no.getText())+"的学生", "删除成功",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}	
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void Reset()
	{
		no.setText("");
		name.setText("");
		sex.setText("");
		nation.setText("");
		college.setText("");
		specialty.setText("");
		birthday.setText("");
		grade.setText("");		
		no.setEditable(true);  
		delete.setEnabled(false);
	}
	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}
    }  
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }  
}